package text6;

public class IsData {

    public static boolean is_data(String str) {
        // 若字符串为 null 或者为空字符串，直接判定不是数值
        if (str == null || str.isEmpty()) {
            return false;
        }
        boolean hasNumber = false;
        boolean hasDot = false;
        boolean hasE = false;

        // 遍历字符串中的每个字符
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isDigit(c)) {
                hasNumber = true;
            } else if (c == '.' &&!hasDot &&!hasE) {
                hasDot = true;
            } else if ((c == 'e' || c == 'E') &&!hasE && hasNumber) {
                hasE = true;
                if (i == str.length() - 1 || (i + 1 < str.length() &&!Character.isDigit(str.charAt(i + 1)) && str.charAt(i + 1) != '+' && str.charAt(i + 1) != '-')) {
                    return false;
                }
            } else if (c == '+' || c == '-') {
                if (i > 0 && str.charAt(i - 1) != 'e' && str.charAt(i - 1) != 'E') {
                    return false;
                }
            } else {
                return false;
            }
        }
        return hasNumber;
    }
    public static void main(String[] args) {
        System.out.println("测试 is_data 方法:");
        System.out.println("2025: " + is_data("2025"));
        System.out.println("3.14: " + is_data("3.14"));
        System.out.println("+1.23e-4: " + is_data("+1.23e-4"));
        System.out.println("abc: " + is_data("abc"));
        System.out.println("1.2.3: " + is_data("1.2.3"));
        System.out.println("e5: " + is_data("e5"));
        System.out.println("-5e: " + is_data("-5e"));
        System.out.println("123e+: " + is_data("123e+"));
    }
}